package se.topshelf.golftracker.web.controller.memberclub;

import org.springframework.beans.propertyeditors.StringTrimmerEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.Validator;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;
import se.topshelf.golftracker.core.domain.Country;
import se.topshelf.golftracker.core.domain.MemberClub;
import se.topshelf.golftracker.core.service.BaseService;
import se.topshelf.golftracker.core.service.MemberClubService;
import se.topshelf.golftracker.web.controller.BaseEntityController;
import se.topshelf.golftracker.web.editor.BaseEntityEditor;

import javax.annotation.Resource;

/**
 *
 */
@Controller
@SessionAttributes("entity")
@RequestMapping("/memberclub")
public class MemberClubController extends BaseEntityController<MemberClub> {
    static final String ALL_COUNTRIES_MODEL_ATTRIBUTE = "allCountries";
    @Resource  // Automatically set ("wired") by Spring
    private MemberClubService memberClubService;

    public MemberClubController() {
        super(MemberClub.class);
    }

    @InitBinder
    protected void initBinder(WebDataBinder binder) {
        binder.registerCustomEditor(String.class, new StringTrimmerEditor(true));
        binder.registerCustomEditor(Country.class, "address.country", new BaseEntityEditor<Country>(Country.class, memberClubService));
    }

    @Override
    protected Validator getValidator() {
        return new MemberClubValidator(memberClubService);
    }

    @Override
    protected String getEntityUrlContextPath() {
        return "memberclub";
    }

    @Override
    protected BaseService getBaseService() {
        return memberClubService;
    }

    @Override
    protected void addModelReferenceData(ModelMap model, MemberClub entity) {
        model.put(ALL_COUNTRIES_MODEL_ATTRIBUTE, super.fetchAllEntitiesAndBuildReferenceDataFor(Country.class));

    }
}
